package org.jinyan.unused;

import java.util.HashMap;

import org.jinyan.led.model.Device;

public class MyLEDCacheManager {
	private static HashMap<String, Device> cacheMap = new HashMap<String, Device>();

	private MyLEDCacheManager() {

	}

	public static synchronized void putLed(String key, Device obj) {
		cacheMap.put(key, obj);
		System.out.println("添加:" + obj);
	}

	public static synchronized Device getLed(String key) {
		if (cacheMap.containsKey(key)) {
			System.out.println("获取:" + cacheMap.get(key));
			return cacheMap.get(key);
		} else {
			return null;
		}
	}

	public static synchronized Device getLedByName(String key) {
		if (!cacheMap.isEmpty()) {
			for (Device obj : cacheMap.values()) {
				if ("led1".equals(obj.getNumber())) {
					return obj;
				}
				if ("led2".equals(obj.getNumber())) {
					return obj;
				}
				if ("led3".equals(obj.getNumber())) {
					return obj;
				}
				if ("led4".equals(obj.getNumber())) {
					return obj;
				}
			}

			return cacheMap.get(key);
		} else {
			return null;
		}
	}

	public static synchronized Device clearLed(String key) {
		if (cacheMap.containsKey(key)) {
			return cacheMap.remove(key);
		} else {
			return null;
		}
	}

	public static synchronized void clearAllLed() {
		if (!cacheMap.isEmpty()) {
			cacheMap.clear();
		}
	}

}
